#include <stdlib.h>
#include <stdio.h>
#include <rid.h>

#include "../unfy.h"
#include "../unfp.h"

int
main(int args, char *argv[])
{
	Unfy_recycle rec;
	const char *error;
	size_t pos = 0;
	Unfy_term *term;
	Unfy_term *term2;

	unsigned char *buf = NULL;
	size_t size = 0;
	size_t len = 0;

	Unfy_bind *lbind = NULL;
	Unfy_bind *lbind2 = NULL;
	Unfy_bind *rbind = NULL;
	Unfy_bind *rbind2 = NULL;
	Unfy_info info;

	unfy_recycle_init(&rec);
	term = unfp_term_parse("(const man_________________ "
			       "() "
			       "var can_________________ "
			       "_ "
			       "order 1 order 2 order 2 "
			       "(const socrates____________))",
			       &pos, &error, &rec);

	unfp_term_ser(term, &buf, &size, &len);
	len = 0;

	if (unfp_term_deserable(buf, &len, size))
		printf("Safe to deser term!\n");

	len = 0;
        unfp_term_deser(&term2, buf, &len, &rec);
	unfp_term_print(term2);
	printf("\n");
	unfy_recycle_term(&rec, term2);

	term2 = unfp_term_parse("(var var_________________ "
				"() "
				"var tan_________________ "
				"const useless_____________ "
				"order 1 order 2 order 2 "
				"(const socrates____________))",
				&pos, &error, &rec);

	unfy_info_init(&info, term, term2);
	unfy_unify(&info, &rec);
	unfp_bind_ser(lbind, &buf, &size, &len);
	len = 0;

	if (unfp_bind_deserable(buf, &len, size))
		printf("Safe to deser lbind!\n");

	unfp_bind_deser(&lbind2, buf, &len, &rec);
	unfp_bind_print(lbind2);
	unfp_bind_ser(rbind, &buf, &size, &len);
	len = 0;

	if (unfp_bind_deserable(buf, &len, size))
		printf("Safe to deser rbind!\n");

	unfp_bind_deser(&rbind2, buf, &len, &rec);
	unfp_bind_print(rbind2);

	free(buf);
	unfy_recycle_bind(&rec, lbind);
	unfy_recycle_bind(&rec, lbind2);
	unfy_recycle_bind(&rec, rbind);
	unfy_recycle_bind(&rec, rbind2);
	unfy_recycle_term(&rec, term);
	unfy_recycle_term(&rec, term2);
	unfy_recycle_empty(&rec);
	return 0;
}
